package com.ruoyi.project.ledger.controller;

import java.util.List;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.security.service.PermissionService;
import com.ruoyi.project.system.domain.SysDept;
import com.ruoyi.project.system.domain.SysUser;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.ledger.domain.LedgerSpecialActivity;
import com.ruoyi.project.ledger.service.ILedgerSpecialActivityService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 专项活动Controller
 *
 * @author ruoyi
 * @date 2020-06-14
 */
@RestController
@RequestMapping("/ledger/activity")
public class LedgerSpecialActivityController extends BaseController
{
    @Autowired
    private ILedgerSpecialActivityService ledgerSpecialActivityService;
    @Autowired
    private PermissionService permissionService;

    /**
     * 查询专项活动列表
     */
//    @PreAuthorize("@ss.hasPermi('ledger:activity:list')")
    @GetMapping("/list")
    public TableDataInfo list(LedgerSpecialActivity ledgerSpecialActivity)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        // 场站id
        Long stationId = user.getStationId();
        if(stationId != null){
            ledgerSpecialActivity.setStationId(stationId);
        }
        // 管辖区域
        List<String> areaList = user.getAreaList();
        String areas = String.join(",", areaList);
        String managementCharacters = user.getManagementCharacters();
        ledgerSpecialActivity.setAreas(areas);
        ledgerSpecialActivity.setManagementCharacters(managementCharacters);
        startPage();
        List<LedgerSpecialActivity> list = ledgerSpecialActivityService.selectLedgerSpecialActivityList(ledgerSpecialActivity);
        return getDataTable(list);
    }

    /**
     * 导出专项活动列表
     */
//    @PreAuthorize("@ss.hasPermi('ledger:activity:export')")
    @Log(title = "专项活动", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(LedgerSpecialActivity ledgerSpecialActivity)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        // 场站id
        Long stationId = user.getStationId();
        if(stationId != null){
            ledgerSpecialActivity.setStationId(stationId);
        }
        // 管辖区域
        List<String> areaList = user.getAreaList();
        String areas = String.join(",", areaList);
        String managementCharacters = user.getManagementCharacters();
        ledgerSpecialActivity.setAreas(areas);
        ledgerSpecialActivity.setManagementCharacters(managementCharacters);
        List<LedgerSpecialActivity> list = ledgerSpecialActivityService.selectLedgerSpecialActivityList(ledgerSpecialActivity);
        ExcelUtil<LedgerSpecialActivity> util = new ExcelUtil<LedgerSpecialActivity>(LedgerSpecialActivity.class);
        return util.exportExcel(list, "activity");
    }

    /**
     * 获取专项活动详细信息
     */
//    @PreAuthorize("@ss.hasPermi('ledger:activity:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(ledgerSpecialActivityService.selectLedgerSpecialActivityById(id));
    }

    /**
     * 新增专项活动
     */
    @PreAuthorize("@ss.hasPermi('ledger:activity:add')")
    @Log(title = "专项活动", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LedgerSpecialActivity ledgerSpecialActivity)
    {
        // 判断是否是惠山场站角色
        if(!permissionService.hasRole("admin") && permissionService.hasRole("hs_station")){
            SysUser user = SecurityUtils.getLoginUser().getUser();
            Long stationId = user.getStationId();
            ledgerSpecialActivity.setStationId(stationId);
            ledgerSpecialActivity.setCreateId(String.valueOf(user.getUserId()));
        }
        else{
            return AjaxResult.error("当前角色无新增权限");
        }
        return toAjax(ledgerSpecialActivityService.insertLedgerSpecialActivity(ledgerSpecialActivity));
    }

    /**
     * 修改专项活动
     */
    @PreAuthorize("@ss.hasPermi('ledger:activity:edit')")
    @Log(title = "专项活动", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LedgerSpecialActivity ledgerSpecialActivity)
    {
        // 判断是否是惠山场站角色
        if(!permissionService.hasRole("admin") && permissionService.hasRole("hs_station")){
            SysUser user = SecurityUtils.getLoginUser().getUser();
            Long stationId = user.getStationId();
            ledgerSpecialActivity.setStationId(stationId);
            ledgerSpecialActivity.setCreateId(String.valueOf(user.getUserId()));
        }
        else{
            return AjaxResult.error("当前角色无修改权限");
        }
        return toAjax(ledgerSpecialActivityService.updateLedgerSpecialActivity(ledgerSpecialActivity));
    }

    /**
     * 删除专项活动
     */
    @PreAuthorize("@ss.hasPermi('ledger:activity:remove')")
    @Log(title = "专项活动", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(ledgerSpecialActivityService.deleteLedgerSpecialActivityByIds(ids));
    }
}
